package Thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo37 {

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(4);

        //构造方法的数字，就是拆分出来的任务的个数
        CountDownLatch countDownLatch = new CountDownLatch(20);

        for (int i = 0; i < 20; i++) {
            int id = i;
            executorService.submit(() -> {
                System.out.println("下载任务：" + id + "开始执行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("下载任务：" + id + "结束执行");

                //完毕，over!!!
                countDownLatch.countDown();
            });
        }
        //当 countDownLatch 收到了 20 个 “ 完成 ”，所有任务都完成了
        // await => all wait
        // await 这个词也是计算机术语，在 python / js 的意思是 async wait (异步等待)
        countDownLatch.await();

        System.out.println("所有任务都完成");
    }

}
